<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class PagesController extends Controller {

	/**
	 * Controller name
	 *
	 * @var string
	 */
	public $name = 'Pages';

	/**
	 * This controller does not use a model
	 *
	 * @var array
	 */
	public $uses = array();

	/**
	 * Displays a view
	 *
	 * @param mixed What page to display
	 * @return void
	 */
	public function leftMenu(){
		$menu = "";
		$menu .= "<div id='cssmenu'>";
		$menu .=  "<ul>";
		$result = $this->Page->query('select * from news_types where left_menu = 1');
		foreach ($result as $item)
		{
			if ($item["news_types"]["parent"] == null)
			{
				$result2 = $this->Page->query('select * from news_types where parent = '.$item["news_types"]["id"]);
				if ($result2 == null){
					$menu .= "<li class='active'><a href='".$item["news_types"]["alias"]."'><span>".$item["news_types"]["name"]."</span></a></li>";
				}
				else {
					$menu .= "<li class='has-sub'><a href='".$item["news_types"]["alias"]."'><span>".$item["news_types"]["name"]."</span></a>";
					$menu .= "<ul>";
					foreach ($result2 as $item_child){
						$menu .= "<li><a href='".$item_child["news_types"]["alias"]."'><span>".$item_child["news_types"]["name"]."</span></a></li>";
					}
					$menu .= "</ul></li>";
				}
			}
		}
		$menu.= "</ul>";
		$menu.= "</div>";
		return $menu;
	}

	public function newsAndEvent(){
		$newsAndEvent = "";
		$result = $this->Page->query("SELECT * FROM news where news_type_id = 18 limit 0,2");
		foreach ($result as $item) {
			$newsAndEvent .= "<div class='newsTop'>";
			$news_des = substr($item["news"]["description"],0,130);
			while ($news_des[strlen($news_des)-1] != " ")
			{
				$news_des = substr($news_des, 0,strlen($news_des)-1);
			}
			$news_des .= "...";
			$newsAndEvent .= "<div class='news_pic'>"."<img class='news_img' src='".$this->webroot."img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$newsAndEvent .= "<div  class='news_des'>";
			
			$result1 = $this->Page->query("SELECT * FROM news_types where id=".$item["news"]["news_type_id"]);
			foreach ($result1 as $item1) {
				$news_link = $item1["news_types"]["alias"]."/";
			}
			$newsAndEvent .= "<div><a href='".$news_link.$item["news"]["alias"]."' class='news_title'>".$item["news"]["name"]."</a></div>";
			$newsAndEvent .= "<div><p class='news_view'>Lượt xem: "." lần</p></div><br>";
			$newsAndEvent .= "<div><p class='news_content'>".$news_des."</p></div>";
			$newsAndEvent .= "</div>";
			$newsAndEvent .= "</div>";
		}
		$newsAndEvent .= "<ul class='newsListItem'>";
		$result = $this->Page->query("SELECT * FROM news WHERE news_type_id = 18 limit 0,6");
		foreach ($result as $item) {
			$newsAndEvent .= "<li><a class='news_title' href='".$this->getAliasFromNewsID($item["news"]["id"])."/".$item["news"]["alias"]."'>".$item["news"]["name"]."</a></li>";
		}
		$newsAndEvent .= "</ul>";
		$newsAndEvent .= "<ul class='newsListItem'>";
		$result = $this->Page->query("SELECT * FROM news WHERE news_type_id = 18 limit 7,12");
		foreach ($result as $item) {
			$newsAndEvent .= "<li><a class='news_title' href='".$this->getAliasFromNewsID($item["news"]["id"])."/".$item["news"]["alias"]."'>".$item["news"]["name"]."</a></li>";
		}
		$newsAndEvent .= "</ul>";
		return $newsAndEvent;
	}
	
	public function newsContainer(){
		$newsContainer = "<div>";
		$result = $this->Page->query("SELECT * FROM news limit 0,3");
		foreach ($result as $item) {
			$news_des = substr($item["news"]["description"],0,250);
			$result2 = $this->Page->query("SELECT news_types.alias FROM news,news_types where news.news_type_id = news_types.id and news.id=".$item["news"]["id"]);
			$types_alias = "";
			foreach ($result2 as $item2) {
				$type_alias = $item2["news_types"]["alias"];
			}
			while ($news_des[strlen($news_des)-1] != " ")
			{
				$news_des = substr($news_des, 0,strlen($news_des)-1);
			}
			$news_des .= "...";
			$newsContainer .= "<div class='news_pic'>"."<img class='news_img' src='".$this->webroot."img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$newsContainer .= "<div  class='news_des'>";
			$newsContainer .= "<div><a class='news_title' href='".$this->getAliasFromNewsID($item["news"]["id"])."/".$item["news"]["alias"]."'>".$item["news"]["name"]."</a></div>";
			$newsContainer .= "<div><p class='news_date'>Ngày đăng: ".date("d/m/Y",strtotime($item["news"]["date"]))."</p></div><br>";
			$newsContainer .= "<div><p class='news_view'>Lượt xem: "." lần</p></div><br>";
			$newsContainer .= "<div><p class='news_content'>".$news_des."</p></div>";
			$newsContainer .= "<div><p class='news_more'><a class='news_more' href='".$type_alias."/".$item["news"]["alias"]."'>>> Xem thêm</a></p></div>";
			$newsContainer .= "</div>";
		}
		$newsContainer .= "</div>";
		return $newsContainer;
	}
	
	
	public function introduce() {
		$introduce = "<div>";
		$resuslt = $this->Page->query("SELECT * FROM news WHERE id = 6");
		foreach ($resuslt as $item)
		{
			$result2 = $this->Page->query("SELECT news_types.alias FROM news,news_types where news.news_type_id = news_types.id and news.id=".$item["news"]["id"]);
			$types_alias = "";
			foreach ($result2 as $item2) {
				$type_alias = $item2["news_types"]["alias"];
			}
			$news_des = substr($item["news"]["description"],0,350);
			while ($news_des[strlen($news_des)-1] != " ")
			{
				$news_des = substr($news_des, 0,strlen($news_des)-1);
			}
			$news_des .= "...";
			$introduce .= "<div class='introduce_pic'>"."<img class='introduce_img' src='".$this->webroot."/img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$introduce .= "<div class='introduce_des'><p class='introduce_content'>".$news_des."</p></div>";
			$introduce .= "<div><p class='news_more'><a class='news_more' href='".$type_alias."/".$item["news"]["alias"]."'>>> Xem thêm</a></p></div>";

				
		}
		$introduce .= "</div>";
		return $introduce;
	}
	
	public function project(){
		$project  = "<div>";
		$result = $this->Page->query("SELECT * FROM news where news_type_id = 100 limit 0,3 ");
		foreach ($result as $item) {
			$news_des = substr($item["news"]["description"],0,150);
			while ($news_des[strlen($news_des)-1] != " ")
			{
				$news_des = substr($news_des, 0,strlen($news_des)-1);
			}
			$project .= "<div class='project_pic'>"."<img class='project_img' src='".$this->webroot."img/news/".md5($item["news"]["id"]).".jpg'/></div>";
			$project .= "<div  class='projects_des'>";
			$project .= "<div><p class='project_title'>".$item["news"]["name"]."</p></div>";
			$project .= "<div><p class='project_content'>".$news_des."</p></div>";
			$project .= "<div><p class='project_view'>Lượt xem: "." lần</p></div><br>";
			$project .= "</div>";
		}
		$project  .= "</div>";
		return $project;
	}
	
	public function display() {
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			$this->redirect('/');
		}
		$page = $subpage = $title_for_layout = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
		$this->set(compact('page', 'subpage', 'title_for_layout'));
		$this->set('left_menu',$this->leftMenu());
		$this->set('newsContainer',$this->newsContainer());
		$this->set('newsAndEvent',$this->newsAndEvent());
		$this->set('Introduce',$this->introduce());
		$this->set('project',$this->project());


		$this->render(implode('/', $path));
	}
	
	private function getAliasFromNewsID($id){
		$result = $this->Page->query("SELECT news_types.alias FROM news,news_types WHERE news.news_type_id = news_types.id");
		foreach ($result as $item){
			return $item["news_types"]["alias"];
		}
	}
	

}
